package com.res.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.res.entity.Computer;
import com.res.entity.Register;
import com.res.entity.Static;
import com.res.service.ComputerService;
import com.res.service.RegisterService;
import com.res.service.StaticService;
import com.res.util.FenyeUtil;

@RestController
@RequestMapping("static")
public class StaticController {
@Autowired
private StaticService staticService;
@Autowired
private ComputerService computerService;
@Autowired
private RegisterService registerService;
@GetMapping
public FenyeUtil<Static>GetAll(Integer page){
	FenyeUtil<Static> f= new FenyeUtil<Static>();
	Pageable pageable=PageRequest.of(page-1, 10);
	Page<Static> p=staticService.findAll(pageable);
	int count=(int)p.getTotalElements();
	f.setCode(0);
	f.setData(p.getContent());
	f.setCount(count);
	//用来接收刚才原生sql查询的数据
	List<Register> list=registerService.all();
	//用来接收刚才原生sql查询的数据
	List<Static> slist=staticService.all();
	//用来接收每个机房的使用次数,因为是多个机房 所以用list
	List<Integer>nlist=registerService.num();
	//在for循环里面用来暂时存放每个机房的使用次数
	int num=0;
	for(int i=0;i<nlist.size();i++) {
		//computer表里面有rid,static中也有rid,rid作为判断条件,如果rid相等,就说明数据是对应的
		//就把这个使用次数给赋值到static中的pnum里面
  		if(list.get(i).getComputer().getReadRoom().getRid()==slist.get(i).getReadRoom().getRid()) {		
			num=nlist.get(i);
		 	//一个修改数据的方法
				staticService.UpdatePnum(num,slist.get(i).getSid());	
}
	}

   
    return f;
}
}
